package ink.xiaobaibai.controller.warehouse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ink.xiaobaibai.autoConfig.roleResources.ResourcesRoleMapping;
import ink.xiaobaibai.common.LogisticsBackStatus;
import ink.xiaobaibai.common.MyRole;
import ink.xiaobaibai.common.PageInfo;
import ink.xiaobaibai.common.WareLogisticsBo;
import ink.xiaobaibai.entity.*;
import ink.xiaobaibai.enumOxy.MonthlyfeeOrderStatus;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @description: 仓库管理员接口
 * @author: 小白白
 * @create: 2021-07-27
 **/

@RequestMapping("/warehouse")
@Api(tags = "仓库管理员接口")
@RestController
@CrossOrigin
@ResourcesRoleMapping(resourceName = "仓库管理员", includeRoleNames = {MyRole.ADMIN, MyRole.POWER_WAREHOUSE})
public class WarehouseController {

    @Autowired
    private IMonthlyfeeOrderService monthlyfeeOrderService;

    @Autowired
    private ILogisticsService logisticsService;

    @Autowired
    private IMonthlyfeeDeviceService monthlyfeeDeviceService;

    @Autowired
    private IMonthlyOxygenDeviceService monthlyOxygenDeviceService;

    @Autowired
    private IBuyOrderService buyOrderService;

    @Autowired
    private ILogisticsBuyService logisticsBuyService;

    @Autowired
    private IBuyOzoneDeviceService buyOzoneDeviceService;

    @Autowired
    private IBuyOxygenDeviceService buyOxygenDeviceService;

    @GetMapping("/list-monthly")
    @ApiOperation("查看包月业务的物流信息")
    public ResponseFormat listMonthly(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize,
                                      @RequestParam("orderId") String orderId) {
        QueryWrapper<Logistics> q1 = new QueryWrapper<>();
        if (orderId != null) {
            q1.eq("monthly_order_id", orderId);
        }
        Page<Logistics> page = this.logisticsService.page(new Page<>(pageNum, pageSize), q1);
        return ResponseFormat.success(page);
    }

    /**
     * 查看当前未发货的物流信息
     */
    @GetMapping("/list-no-send-monthly")
    @ApiOperation("查看包月被管理审核通过但是未发货的物流信息")
    public ResponseFormat listMonthlyNoSend(@RequestParam("pageNum") Integer pageNum,
                                            @RequestParam("pageSize") Integer pageSize) {
        PageInfo<Logistics> pageInfo = this.monthlyfeeOrderService.getLogisticsListByNoSend(pageNum, pageSize);
        return ResponseFormat.success(pageInfo);
    }

    @GetMapping("/orderItem-monthly")
    @ApiOperation("查看包月订单详细 | 传入[list-monthly接口中的monthly_order_id]包月订单id | 返回负责人id 和 机器的信息")
    public ResponseFormat orderItemMonthly(@RequestParam("orderId") String orderId) {
        QueryWrapper<MonthlyfeeOrder> q1 = new QueryWrapper<>();
        q1.select("admin_user_id", "device_id");
        q1.eq("order_id", orderId);
        MonthlyfeeOrder order = this.monthlyfeeOrderService.getOne(q1);
        if (order == null) {
            return ResponseFormat.fail("当前订单不存在");
        }
        MonthlyfeeDevice monthlyfeeDevice = this.monthlyfeeDeviceService.getById(order.getDeviceId());
        MonthlyOxygenDevice monthlyOxygenDevice = this.monthlyOxygenDeviceService.getById(monthlyfeeDevice.getOxygenId());
        order.setDeviceId(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("adminUserId", order.getAdminUserId());
        jsonObject.put("ozone", monthlyfeeDevice);
        jsonObject.put("oxygen", monthlyOxygenDevice);
        return ResponseFormat.success(jsonObject);
    }

    @PostMapping("/addMonthlyLogistics")
    @ApiOperation("填写物流信息(这个接口也是修改的接口,再次添加就是覆盖) | 填写 物流订单号,一张图片凭证,发送费用 (没有就填写 '无' )")
    public ResponseFormat addMonthlyLogistics(@RequestBody @Validated WareLogisticsBo wareLogisticsBo) {
        Integer logisticsId = wareLogisticsBo.getLogisticsId();
        UpdateWrapper<Logistics> u1 = new UpdateWrapper<>();
        u1.set("number", wareLogisticsBo.getNumber());
        u1.set("images_uri", wareLogisticsBo.getImageUrl());
        u1.set("send_cost", wareLogisticsBo.getSendCost());
        u1.eq("id", logisticsId);
        this.logisticsService.update(u1);
        return ResponseFormat.success();
    }

    @PostMapping("/okMonthlyLogistics")
    @ApiOperation("将当前包月物流订单变为发货状态")
    public ResponseFormat okMonthlyLogistics(@RequestBody Integer id) {
        UpdateWrapper<Logistics> u1 = new UpdateWrapper<>();
        u1.set("send", true);
        u1.eq("id", id);
        this.logisticsService.update(u1);
        return ResponseFormat.success();
    }

    @GetMapping("/return-monthly-list")
    @ApiOperation("查看当前申请退还押金的订单 | 因为申请退还押金的订单才是准备回退的订单")
    public ResponseFormat returnMonthlyList(@RequestParam("pageNum") Integer pageNum,
                                            @RequestParam("pageSize") Integer pageSize) {
        QueryWrapper<MonthlyfeeOrder> q1 = new QueryWrapper<>();
        q1.eq("status", MonthlyfeeOrderStatus.APDEPOSIT.getStatus());
        Page<MonthlyfeeOrder> page = this.monthlyfeeOrderService.page(new Page<>(pageNum, pageSize), q1);
        return ResponseFormat.success(page);
    }

    @GetMapping("/return-monthly")
    @ApiOperation("传入回收费用(可选,默认0)+回收图片凭证(可选)")
    public ResponseFormat returnMonthly(@RequestParam("logisticsId") Integer logisticsId,
                                        @RequestParam("imageUrl") String imageUrl,
                                        @RequestParam("cost") BigDecimal cost) {
        UpdateWrapper<Logistics> u1 = new UpdateWrapper<>();
        u1.set("income_cost", cost);
        u1.set("images_uri2", imageUrl);
        u1.eq("id", logisticsId);
        this.logisticsService.update(u1);
        return ResponseFormat.success();
    }

    @PostMapping("/ok-return-monthly")
    @ApiOperation("传入物流id,为其对应的物流订单打上标识,表明当前月费订单的机器已经退回 |  标识作用,表明当前订单的机器已经退回,供管理员参看,然后管理员就允许退押金了")
    public ResponseFormat okReturnMonthly(@RequestBody Integer logisticsId) {
        UpdateWrapper<Logistics> u1 = new UpdateWrapper<>();
        u1.set("back_status", LogisticsBackStatus.YES.getStatus());
        u1.eq("id", logisticsId);
        this.logisticsService.update(u1);
        return ResponseFormat.success();
    }

    @PostMapping("/ignore-return-monthly")
    @ApiOperation("[修改成忽略状态]传入物流id,为其对应的物流订单打上标识,表明当前月费订单的机器已经退回 |  标识作用,表明当前订单的机器不做提醒,供管理员参看,然后管理员就允许退押金了")
    public ResponseFormat ignoreReturnMonthly(@RequestBody Integer logisticsId) {
        UpdateWrapper<Logistics> u1 = new UpdateWrapper<>();
        u1.set("back_status", LogisticsBackStatus.IGNORE.getStatus());
        u1.eq("id", logisticsId);
        this.logisticsService.update(u1);
        return ResponseFormat.success();
    }

    @GetMapping("/list-buy")
    @ApiOperation("查看购买业务的物流信息")
    public ResponseFormat listBuy(@RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize,
                                  @RequestParam("orderId") String orderId) {
        QueryWrapper<LogisticsBuy> q1 = new QueryWrapper<>();
        if (orderId != null) {
            q1.eq("buy_order_id", orderId);
        }
        Page<LogisticsBuy> page = this.logisticsBuyService.page(new Page<>(pageNum, pageSize), q1);
        return ResponseFormat.success(page);
    }

    /**
     * 查看当前未发货的物流信息
     */
    @GetMapping("/list-no-send-buy")
    @ApiOperation("查看购买被管理审核通过但是未发货的物流信息")
    public ResponseFormat listBuyNoSend(@RequestParam("pageNum") Integer pageNum,
                                        @RequestParam("pageSize") Integer pageSize) {
        PageInfo<LogisticsBuy> pageInfo = this.buyOrderService.getLogisticsListByNoSend(pageNum, pageSize);
        return ResponseFormat.success(pageInfo);
    }

    @GetMapping("/orderItem-buy")
    @ApiOperation("查看购买订单详细 | 传入[list-buy接口中的buy_order_id]包月订单id | 返回负责人id 和 机器的信息")
    public ResponseFormat orderItemBuy(@RequestParam("orderId") String orderId) {
        QueryWrapper<BuyOrder> q1 = new QueryWrapper<>();
        q1.select("admin_user_id", "device_id");
        q1.eq("order_id", orderId);
        BuyOrder order = this.buyOrderService.getOne(q1);
        if (order == null) {
            return ResponseFormat.fail("当前订单不存在");
        }
        BuyOzoneDevice ozoneDevice = this.buyOzoneDeviceService.getById(order.getDeviceId());
        BuyOxygenDevice oxygenDevice = this.buyOxygenDeviceService.getById(ozoneDevice.getOxygenId());
        order.setDeviceId(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("adminUserId", order.getAdminUserId());
        jsonObject.put("ozone", ozoneDevice);
        jsonObject.put("oxygen", oxygenDevice);
        return ResponseFormat.success(jsonObject);
    }

    @PostMapping("/addBuyLogistics")
    @ApiOperation("填写物流信息(这个接口也是修改的接口,再次添加就是覆盖) | 填写 物流订单号,一张图片凭证,发送费用 (没有就填写 '无' )")
    public ResponseFormat addBuyLogistics(@RequestBody @Validated WareLogisticsBo wareLogisticsBo) {
        Integer logisticsId = wareLogisticsBo.getLogisticsId();
        UpdateWrapper<LogisticsBuy> u1 = new UpdateWrapper<>();
        u1.set("number", wareLogisticsBo.getNumber());
        u1.set("image_url", wareLogisticsBo.getImageUrl());
        u1.set("send_cost", wareLogisticsBo.getSendCost());
        u1.eq("id", logisticsId);
        this.logisticsBuyService.update(u1);
        return ResponseFormat.success();
    }

    @PostMapping("/okBuyLogistics")
    @ApiOperation("将当前购买物流订单变为发货状态")
    public ResponseFormat okBuyLogistics(@RequestBody Integer id) {
        UpdateWrapper<LogisticsBuy> u1 = new UpdateWrapper<>();
        u1.set("send", true);
        u1.eq("id", id);
        this.logisticsBuyService.update(u1);
        return ResponseFormat.success();
    }

}
